package com.coin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.SysPrivilege;
import com.coin.mapper.SysPrivilegeMapper;
import com.coin.service.SysPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 李力
 * @description 针对表【sys_privilege(权限配置)】的数据库操作Service实现
 * @createDate 2025-05-24 17:08:03
 */
@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege>
        implements SysPrivilegeService {


    @Autowired
    private SysPrivilegeMapper sysPrivilegeMapper;

    /**
     * @Description: 根据角色, 菜单查询当前菜单下所有的权限
     * @Author: 李 力
     * @Date: 2025/5/27
     * @Param: menuId, roleId
     * @return: List
     */
    @Override
    public List<SysPrivilege> getAllPermissions(Long menuId, Long roleId) {
        //1.查询该菜单下的所有权限
        LambdaQueryWrapper<SysPrivilege> wrapper = new LambdaQueryWrapper<>();
        wrapper = wrapper.eq(SysPrivilege::getMenuId, menuId);
        List<SysPrivilege> sysPrivileges = list(wrapper);
        if (sysPrivileges.isEmpty()) {
            return sysPrivileges;
        }
        //2.当前传递的角色是否包含这样的角色
        Set<Long> ids = sysPrivilegeMapper.getPrivilegeIdsByRoleId(roleId);
        for (SysPrivilege sysPrivilege : sysPrivileges) {
            if (ids.contains(sysPrivilege.getId())) {
                sysPrivilege.setOwn(1);
            }
        }
        return sysPrivileges;
    }
}




